/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Utilities
*        文件名:             BaseExportExcel.cs
*        创建系统时间:       2012/11/2 19:07:07
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Utilities
{
    using System;
    using System.Data;
    using System.IO;
    using System.Web;

    public class BaseExportExcel
    {
        private void DeleteExistFile(string fileName)
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
        }

        public void GetExcelFile(System.Data.DataTable dataTable, string templateFilePath, string outFilePath, int startRowIndex, int startColIndex, string downloadPath)
        {
            int num = 0;
            int num2 = 0;
            int count = dataTable.Rows.Count;
            int num4 = dataTable.Columns.Count + 1;
            DataView defaultView = dataTable.DefaultView;
            templateFilePath = HttpContext.Current.Server.MapPath(templateFilePath);
            string fileName = HttpContext.Current.Server.MapPath(outFilePath);

            object[,] objArray = new object[count, num4];
            foreach (DataRowView view2 in defaultView)
            {
                num = 0;
                foreach (DataColumn column in defaultView.Table.Columns)
                {
                    if (num == 0)
                    {
                        objArray[num2, num] = num2 + 1;
                        num++;
                    }
                    if (column.DataType == Type.GetType("System.DateTime"))
                    {
                        if (view2[column.ColumnName].ToString().Length > 0)
                        {
                            objArray[num2, num] = Convert.ToDateTime(view2[column.ColumnName].ToString()).ToString();
                        }
                        else
                        {
                            objArray[num2, num] = string.Empty;
                        }
                    }
                    else if (column.DataType == Type.GetType("System.String"))
                    {
                        objArray[num2, num] = "'" + view2[column.ColumnName].ToString();
                    }
                    else
                    {
                        objArray[num2, num] = view2[column.ColumnName].ToString();
                    }
                    num++;
                }
                num2++;
            }
            this.DeleteExistFile(fileName);

            HttpContext.Current.Response.Redirect(downloadPath);
        }
    }
}